package cn.youqu.happy.module.youqu.controller.app.userActivity;

import cn.youqu.happy.framework.common.pojo.CommonResult;
import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.security.core.annotations.PreAuthenticated;
import cn.youqu.happy.module.youqu.controller.admin.userActivity.vo.UserActivityPageReqVO;
import cn.youqu.happy.module.youqu.controller.app.userActivity.vo.*;
import cn.youqu.happy.module.youqu.convert.userActivity.UserActivityConvert;
import cn.youqu.happy.module.youqu.dal.dataobject.userActivity.UserActivityDO;
import cn.youqu.happy.module.youqu.service.userActivity.UserActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.youqu.happy.framework.common.pojo.CommonResult.success;
import static cn.youqu.happy.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Api(tags = "用户 APP - 用户发起活动")
@RestController
@RequestMapping("/youqu/user-activity")
@Validated
public class AppUserActivityController {

    @Resource
    private UserActivityService userActivityService;

    @PostMapping("/publish")
    @ApiOperation("用户发起活动组队")
    @PreAuthenticated
    public CommonResult<Integer> publishUserActivity(@Valid @RequestBody AppUserActivityCreateReqVO createReqVO) {
        Long userId = getLoginUserId();
        return success(userActivityService.publishUserActivity(createReqVO, userId));
    }


    @PostMapping("/join")
    @ApiOperation("用户加入活动组队")
    @PreAuthenticated
    public CommonResult<Boolean> joinUserActivity(@Valid @RequestBody AppUserActivityJoinReqVO joinReqVO) {
        Long userId = getLoginUserId();
        return success(userActivityService.joinUserActivity(joinReqVO.getId(), userId));
    }

    @PostMapping("/quick-join")
    @ApiOperation("用户快速加入活动组队")
    @PreAuthenticated
    public CommonResult<Integer> quickJoinUserActivity(@Valid @RequestBody AppUserActivityQuickJoinReqVO joinReqVO) {
        Long userId = getLoginUserId();
        return success(userActivityService.quickJoinUserActivity(joinReqVO, userId));
    }

    @PostMapping("/kick")
    @ApiOperation("用户踢人出队")
    @PreAuthenticated
    public CommonResult<Boolean> kickUserActivity(@Valid @RequestBody AppUserActivityKickReqVO kickReqVO) {
        Long userId = getLoginUserId();
        return success(userActivityService.kickPartnerUserActivity(kickReqVO.getId(), kickReqVO.getUid(), userId));
    }


    @PostMapping("/force-success")
    @ApiOperation("用户强制成功组队")
    @PreAuthenticated
    public CommonResult<Boolean> foreSuccessUserActivity(@Valid @RequestBody AppUserActivityForceSuccessReqVO forceSuccessReqVO) {
        Long userId = getLoginUserId();
        return success(userActivityService.forceSuccessUserActivity(forceSuccessReqVO.getId(), forceSuccessReqVO.getReason(), userId));
    }


    @PutMapping("/update")
    @ApiOperation("更新用户发起活动")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserActivity(@Valid @RequestBody AppUserActivityUpdateReqVO updateReqVO) {
        Long userId = getLoginUserId();
        userActivityService.updateUserActivity(updateReqVO, userId);
        return success(true);
    }


    @PostMapping("/cancel")
    @ApiOperation("用户取消组队活动")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<Boolean> cancelUserActivity(@RequestParam("id") Integer id) {
        Long uid = getLoginUserId();
        return success(userActivityService.cancelUserActivity(id, uid));
    }

    @PostMapping("/quit")
    @ApiOperation("用户退出队伍")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<Boolean> quitUserActivity(@RequestParam("id") Integer id) {
        Long uid = getLoginUserId();
        return success(userActivityService.quitUserActivity(id, uid));
    }


    @GetMapping("/get")
    @ApiOperation("获得用户发起活动")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<AppUserActivityRespVO> getUserActivity(@RequestParam("id") Integer id) {
        Long loginUserId = getLoginUserId();

        UserActivityDO userActivity = userActivityService.getUserActivity(id,loginUserId);
        return success(UserActivityConvert.INSTANCE.convert0(userActivity));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户发起活动列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthenticated
    public CommonResult<List<AppUserActivityRespVO>> getUserActivityList(@RequestParam("ids") Collection<Integer> ids) {

        Long loginUserId = getLoginUserId();
        List<UserActivityDO> list = userActivityService.getUserActivityList(ids,loginUserId);
        return success(UserActivityConvert.INSTANCE.convertList0(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户发起活动分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppUserActivityRespVO>> getUserActivityPage(@Valid AppUserActivityPageReqVO pageVO) {
        UserActivityPageReqVO convert = UserActivityConvert.INSTANCE.convert(pageVO);
        Long loginUserId = getLoginUserId();
        PageResult<UserActivityDO> pageResult = userActivityService.getUserActivityPage(convert,loginUserId);
        return success(UserActivityConvert.INSTANCE.convertPage0(pageResult));
    }


}
